// Filename : dictionary.cpp
// Implemention of class dictionary

#include "dictionary.h"
#include <iterator>

// Find the matched position between keys and values 
int 
Dictionary::findPosition( string keyword, ListType type ) const {
	const vector<string> *pointToList;

	if ( type == KEY ) {
		pointToList = &keys;	
	}
	else {
		pointToList = &values;
	}

	// Find whether the keyword is exist
	int i = 0;
	for ( i = 0; i < pointToList->size(); i++ ) {
		if ( pointToList->at(i) == keyword ) {
			break;
		}
	}

	if ( i >= pointToList->size() ) {
		return ( -1 );
	}
	else {
		return i;
	}
}

// Get the value of key related constly
const string
Dictionary::operator[]( string key ) const {
	int position = findPosition( key, KEY );

	if ( position == -1 ) {
		return string("");
	}
	else {
		return values[position];
	}
}

// Get the value of key related can be changed
string&
Dictionary::operator[]( string key ) {
	int position = findPosition( key, KEY );

	if ( position == -1 ) {
		keys.push_back( key );
		values.push_back( string( "" ) );
		return values[ values.size() - 1 ];
	}
	else {
		return values[position];
	}
}

// End of dictionary.cpp
